package com.xu.controller;

import com.xu.pojo.Detail;
import com.xu.pojo.Invitation;
import com.xu.service.DeService;
import com.xu.service.InService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/invi")
public class InController extends BaseController {
    @Resource
    public InService inService;
    public DeService deService;

@RequestMapping("/getallinv")
    public String getallin(Model model){
    List<Invitation> inviList =  inService.getallin();
    model.addAttribute("inviList",inviList);
    return  "index";
}


    //模糊查询
    @RequestMapping("/like")
    public  String likebytitle(@RequestParam("title") String title, Model model){
        List<Invitation> inviList =  inService.likebytitle(title);
        model.addAttribute("inviList",inviList);
        return "index";


    }

    //  查看回复
    @RequestMapping("/back/{id}")
    public String getbyid(@PathVariable Integer id, Model model, HttpServletRequest request){
   List<Detail> list = inService.getbyid(id);
    model.addAttribute("list",list);
      request.setAttribute("invid",id);
    return  "info";

    }
//    删除
    @RequestMapping("/del/{id}")
    public String delRE(@PathVariable Integer id){


        int aa = deService.delReply(id);
        if (aa>0){
            int i  =  inService.delInvi(id);
        }

        return "info";
    }

    //添加回复

    @RequestMapping("/addre/{invid}")
    public String addre(@PathVariable Integer invid,HttpServletRequest request){
        request.setAttribute("invid",invid);
        System.out.println(invid);
        return "addreyly";
    }

    @RequestMapping("/save")
    public String addRe(Detail reply_detail,Integer invid,String content,String author){
        System.out.println(invid);
        reply_detail.setContent(content);
        reply_detail.setAuthor(author);
        reply_detail.setCreatedate(new Date());
        int i = deService.addReply(reply_detail);
        if (i>0){
            return "redirect:/reply/view/"+invid;
        }else{
            return "addreyly";
        }
    }


}
